$(function() {
	$('#datetimepicker').datetimepicker({
		step : 5
	});
	$('#spinner').spinner({
		min : 1,
		max : 100,
		step : 1
	});

	var dsId = getUrlParam("dsId");
	var service = new EbeimService();
	service.getDataSource(dsId, getDsCallback);

});

var getDsCallback = function(data) {
	var json = eval("(" + data + ")");
	var ds = json.obj;
	if (ds.dsType == 1) {
		$(".db-config").css("display", "block");
		$(".ds-path").css("display", "none");
	} else {
		$(".ds-path").css("display", "block");
		$(".db-config").css("display", "none");
	}

	if (ds.dsType == 2) {
		$(".backup-strategy").css("display", "none");
	}

	if (ds.isOpenLocalBackup == 1) {
		$("#isOpenLocalBackup")[0].checked = true;
	}
	
	if (ds.isEncrypt == "1") {
		$("#isEncrypt")[0].checked = true;
	}
	
	if (ds.dsSecurityRank == "1") {
		$("#dsNeedKey")[0].checked = true;
	}else if (ds.dsSecurityRank == "2") {
		$("#dsNeedCode")[0].checked = true;
	}else if (ds.dsSecurityRank == "3") {
		$("#dsNeedKey")[0].checked = true;
		$("#dsNeedCode")[0].checked = true;
	}
	$('.j-checkbox').iCheck({
		checkboxClass : 'icheckbox_square-blue',
		increaseArea : '20%'
	});

	$("#h_id").val(ds.id);
	$("#dsSecurityRank").val(ds.dsSecurityRank);
	$("#h_config_id").val(ds.configId);
	$("#h_backstrategy_id").val(ds.backupStrategyId);
	$('#dsName').attr("value", ds.name);
	$('#createTime').attr("value", new Date(ds.createTime).toLocaleString());
	$('#description').attr("value", ds.description);
	$('#dsType').val(ds.dsType);
	$('#scanType').attr("value", ds.scanType);
	$('#dsPath').attr("value", ds.root);
	$('#startTime').attr("value", ds.startTime);
	$('#dbUsername').attr("value", ds.dbUsername);
	$('#dbPassword').attr("value", ds.dbPassword);
	$('#dbType').attr("value", ds.dbType);
	$('#dbName').attr("value", ds.dbName);
	$('#dbHost').attr("value", ds.dbHost);
	$('#dbPort').attr("value", ds.dbPort);
	$('#backupPeriod').val(ds.backupPeriod);
	$('#backupCycle').attr("value", ds.backupCycle);
	$('#idScanHideFile').attr("checked", getBool(ds.scanHideFile));
	$('#idRecordCanNotReadFileInfo').attr("checked",
			getBool(ds.recordCanNotReadFileInfo));
	$('#idRecordEmptyDirectory').attr("checked",
			getBool(ds.recordEmptyDirectory));
	$('#idRecordEmptyFile').attr("checked", getBool(ds.recordEmptyFile));
	$('#idVerifyCompleteName').attr("checked", getBool(ds.verifyCompleteName));

	var dt = new Date(ds.backupTime);
	var backupTime = dt.format("yyyy/MM/dd hh:mm");
	$('#datetimepicker').attr("value", backupTime);
	// $('#idVerifyCompleteName').attr("checked", true);
	// $('#idRecordEmptyFile').attr("checked", false);
};

var getBool = function(val) {
	if (val == "1") {
		return true;
	}
	return false;
}

function setDynamicCode() {
	var dynamicCode = $("#dynamicCode").val();
	if (dynamicCode == "") {
		alert("动态验证码不能为空!");
		return;
	}
	saveDataSource(dynamicCode);
}

var callback = function(data) {
	$("#save").removeAttr("disabled");
	var json = eval("(" + data + ")");
	if (json.code == 200 || json.code == "200") {
		alert("保存成功！！");
		self.location = document.referrer;
	} else {
		dsSecurityRankCheck(json.code);
	}
};

var saveDataSource = function(dynamicCode) {
	var isOpenLocalBackup = 0;
	if ($('#isOpenLocalBackup')[0].checked) {
		isOpenLocalBackup = 1;
	}
	
	var dsNeedKey = 0;
	var dsNeedKeyCked = $("#dsNeedKey")[0].checked;
	if(dsNeedKeyCked){
		dsNeedKey = 1;
	}
	var dsNeedCode = 0;
	var dsNeedCodeCked = $("#dsNeedCode")[0].checked;
	if(dsNeedCodeCked){
		dsNeedCode = 1;
	}
	var dsSecurityRank = 0;
	if(dsNeedKey==1 && dsNeedCode==0){
		dsSecurityRank = 1;
	}else if(dsNeedKey==0 && dsNeedCode==1){
		dsSecurityRank = 2;
	}else if(dsNeedKey==1 && dsNeedCode==1){
		dsSecurityRank = 3;
	}
	var dsType = $('#dsType').val();
	var ds = {
		id : $("#h_id").val(),
		description : $('#description').val(),
		dsType : dsType,
		isOpenLocalBackup : isOpenLocalBackup,
		dsSecurityRank : dsSecurityRank,
		dynamicCode : dynamicCode
	};
	var time = $('#datetimepicker').val();
	var backupCycle = $("#backupCycle").val();
	var reg = /^[0-9]*[1-9][0-9]*$/.test(backupCycle); //正整数
	if(!reg && dsType != "2"){
		alert("时间间隔请输入正整数！");
		return;
	}
	var bs = {
		id : $("#h_backstrategy_id").val(),
		type : 1,
		backupTime : new Date(time).getTime(),
		backupCycle : backupCycle,
		backupPeriod : $("#backupPeriod").val(),
	}
	var isEncrypt = 0;
	var isEncryptCked = $("#isEncrypt")[0].checked;
	if(isEncryptCked){
		isEncrypt = 1;
	}
	var config = {
		isEncrypt:isEncrypt,
		configId : $("#h_config_id").val(),
		scanType : $('#scanType').val(),
		scanHideFile : getCheckedValue($('#idScanHideFile')),
		recordCanNotReadFileInfo : getCheckedValue($('#idRecordCanNotReadFileInfo')),
		recordEmptyDirectory : getCheckedValue($('#idRecordEmptyDirectory')),
		recordEmptyFile : getCheckedValue($('#idRecordEmptyFile')),
		verifyCompleteName : getCheckedValue($('#idVerifyCompleteName')),
		dbUsername : $("#dbUsername").val(),
		dbPassword : $("#dbPassword").val(),
		dbHost : $("#dbHost").val(),
		dbPort : $("#dbPort").val(),
		dbName : $("#dbName").val()
	}
	var json = {};
	json["datasource"] = ds;
	json["datasourceConfig"] = config;
	json["backStrategy"] = bs;
	// alert(JSON.stringify(ds));
	$("#save").attr("disabled","disabled");
	var service = new EbeimService();
	service.addDataSource(json, callback);
};
var getCheckedValue = function(chk) {
	if (chk.prop("checked")) {
		return 1;
	}
	return 0;
};
/*
 * 日期格式化
 */
Date.prototype.format = function(format) {
	var o = {
		"M+" : this.getMonth() + 1, // month
		"d+" : this.getDate(), // day
		"h+" : this.getHours(), // hour
		"m+" : this.getMinutes(), // minute
		"s+" : this.getSeconds(), // second
		"q+" : Math.floor((this.getMonth() + 3) / 3), // quarter
		"S" : this.getMilliseconds()
	// millisecond
	}
	if (/(y+)/.test(format))
		format = format.replace(RegExp.$1, (this.getFullYear() + "")
				.substr(4 - RegExp.$1.length));
	for ( var k in o)
		if (new RegExp("(" + k + ")").test(format))
			format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k]
					: ("00" + o[k]).substr(("" + o[k]).length));
	return format;
}
/* 自定义checkbox样式 */
$(function() {
	$('.j-checkbox').iCheck({
		checkboxClass : 'icheckbox_square-blue',
		increaseArea : '20%'
	});
})
